//
//  UploadApiManager.swift
//  Lease_IOS
//
//  Created by Mr.Wang on 2019/7/11.
//  Copyright © 2019 Mr.wang. All rights reserved.
//

import Moya

enum UpLoadApimanager {
    case uploadFile(URL)
    case uploadImage(UIImage)
}

extension UpLoadApimanager: TargetType{
    
    var headers: [String : String]? {
        var header = ["Content-Type": "application/json"]
        
        header["CONSTANT_CURRENT_LANGUAGE"] = AppCache.shared.object(for: APPUserDefault.self).userSelectLanguage?.header ?? "en"
        
        if APPUser.shared.isLogin {
            header[APPUser.shared.userInfo.type] = APPUser.shared.token
        }
        
        return header
    }
    
    var path: String {
        return "photovoltaic/sys-oss/post"
    }
    
    var task: Task {
        switch self {
        case .uploadFile(let file):
            let uploadData = MultipartFormData.init(provider: .file(file), name: "file")
            return .uploadCompositeMultipart([uploadData], urlParameters: [:])
        case .uploadImage(let image):
            guard let data = image.pngData() else { return .requestPlain }
            let uploadData = MultipartFormData.init(provider: MultipartFormData.FormDataProvider.data(data), name: "file")
            return .uploadCompositeMultipart([uploadData], urlParameters: [:])
        }
    }
    
}
